import { STORE_PREFIX } from "@/utils/constant";
import { defineStore } from "pinia";

export const useConfigStore = defineStore(
  `${STORE_PREFIX}_config_store`,
  () => {
    const device = reactive<IConfigDevice>({
      isMobile: false,
      isSafari: true
    });
    const isMobile = computed(() => device.isMobile);
    const isSafari = computed(() => device.isSafari);

    const setDevice = async (cb?: Function) => {
      const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
        navigator.userAgent
      );
      const clientWidth = document.body.clientWidth;
      const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
      device.isMobile = isMobile || clientWidth < 480;
      device.isSafari = isSafari;
      // eslint-disable-next-line @typescript-eslint/no-unused-expressions
      cb && cb(device);
    };
    return {
      device,
      isMobile,
      isSafari,
      setDevice
    };
  },
  {
    persist: {
      storage: sessionStorage
    }
  }
);
